package com.woniuxy.gobang.ui;

import dao.UserDataDAO;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

/**
 * 修改密码窗口
 * 
 * @author 张哲轩
 *
 */
public class ModifyPasswordStage extends Stage {
	private Label lbOriPassword = new Label("请输入原密码");
	private PasswordField  txtOriPassword = new PasswordField();

	
	
	private Label lbPassword = new Label("请输入新密码");
	private PasswordField  txtNewPassword = new PasswordField();
	private Label warnPassword = new Label();
	private Label cfmpwdLbl = new Label("确认新密码:");
	private PasswordField txtCfmPwd = new PasswordField();
	private Label lblCfmPwdMsg = new Label();
	private Label warnOriPassword = new Label();
	
	private ModifyPasswordStage stage =this;
	

	public ModifyPasswordStage() {
		setTitle("修改密码");
		// 设置窗体大小不可调整
		setResizable(false);
		// 创建面板
		Pane pane = new Pane();
		
		lbOriPassword.setPrefSize(90, 20);
		lbOriPassword.setLayoutX(30);
		lbOriPassword.setLayoutY(30);
		
		txtOriPassword.setPromptText("请输入原密码");
		txtOriPassword.setPrefWidth(200);
		txtOriPassword.setLayoutX(150);
		txtOriPassword.setLayoutY(30);
	
		warnOriPassword.setLayoutX(360);
		warnOriPassword.setLayoutY(30);
		
		lbPassword.setPrefSize(90, 20);
		lbPassword.setLayoutX(30);
		lbPassword.setLayoutY(70);
		
		txtNewPassword.setLayoutX(150);
		txtNewPassword.setPrefWidth(200);
		txtNewPassword.setLayoutY(70);
		txtNewPassword.setPromptText("请输入新密码");
		
		warnPassword.setLayoutX(360);
		warnPassword.setLayoutY(70);
		
		
		cfmpwdLbl.setPrefSize(90, 20);
		cfmpwdLbl.setLayoutX(30);
		cfmpwdLbl.setLayoutY(110);

		txtCfmPwd.setLayoutX(150);
		txtCfmPwd.setLayoutY(110);
		txtCfmPwd.setPrefWidth(200);
		txtCfmPwd.setPromptText("确认新密码");
		
		lblCfmPwdMsg.setLayoutX(360);
		lblCfmPwdMsg.setLayoutY(110);

		
		
		Button confirmBtn = new Button("确认");
		confirmBtn.setPrefSize(100, 30);
		confirmBtn.setLayoutX(40);
		confirmBtn.setLayoutY(150);
		pane.getChildren().add(confirmBtn);
		
		confirmBtn.setOnMouseClicked(new EventHandler<Event>() {

			@Override
			public void handle(Event event) {
				if(!(checkOriginPassword() && validatePwd() && validateCfmPwd())) {
					return;
				}
				
				try {
					new UserDataDAO().updatePwd(Global.user.getId(),txtNewPassword.getText());
				} catch (Exception e) {
					Alert alert = new Alert(AlertType.INFORMATION);
					alert.setContentText("系统维护中");
					alert.show();
				}

				Alert alert = new Alert(AlertType.INFORMATION);
				// 设置提示框内容
				alert.setContentText("修改成功！");
				// 设置提示框标题
				alert.setTitle("修改结果");
				// 展示弹框
				alert.show();
				
				stage.close();
			}
		});
		
		// 取消按钮
		Button cancelBtn = new Button("取消");
		cancelBtn.setPrefSize(100, 30);
		cancelBtn.setLayoutX(160);
		cancelBtn.setLayoutY(150);
		pane.getChildren().add(cancelBtn);
		// 取消按钮事件
		cancelBtn.setOnMouseClicked(new EventHandler<Event>() {
			@Override
			public void handle(Event event) {
				stage.close();
			}
		});
		
		
		
		
		
		pane.getChildren().addAll(lbOriPassword, txtOriPassword, lbPassword,txtNewPassword,warnPassword,cfmpwdLbl,txtCfmPwd,lblCfmPwdMsg,warnOriPassword);
		
		// 创建一个场景对象
		Scene scene = new Scene(pane, 550, 200);// 控制窗体尺寸
		// 将场景添加到窗体上
		setScene(scene);
		show();
		
	}
	
	//校验密码格式
	private boolean validatePwd() {
		String password = txtNewPassword.getText();
		if (password.trim().equals("")) {
			warnPassword.setText("密码不能为空");
			warnPassword.setTextFill(Color.RED);
			return false;
		} else if (password.trim().length() < 6 || password.trim().length() > 20) {
			warnPassword.setText("密码长度不能小于6或大于20");
			warnPassword.setTextFill(Color.RED);
			return false;
		} else {
			warnPassword.setText("密码有效");
			warnPassword.setTextFill(Color.GREEN);
			return true;
		}
	}
	/**
	 * 检查原密码是否输入正确
	 * @return 正确返回true
	 */
	private boolean checkOriginPassword() {
		if(txtOriPassword.getText().equals(Global.user.getPassword())) {
			warnOriPassword.setText("原密码正确");
			warnOriPassword.setTextFill(Color.GREEN);
			return true;
		}else {
			warnOriPassword.setText("原密码错误");
			warnOriPassword.setTextFill(Color.RED);
		}
		return false;
	}
	/**
	 * 检查两次密码是否一致
	 * @return 一致返回true
	 */
	private boolean validateCfmPwd() {
		String password = txtNewPassword.getText();
		String cfmPwd = txtCfmPwd.getText();
		if (!password.equals(cfmPwd)) {
			lblCfmPwdMsg.setText("确认密码与密码不一致");
			lblCfmPwdMsg.setTextFill(Color.RED);
			return false;
		} else {
			lblCfmPwdMsg.setText("");
			return true;
		}
	}
}
